$(document).ready(function() {
	var str_urlOfContractPhoto=contextPath+'/photo/project/contractPhotoList.json';
	var object_postData={'projectId':1};

	//请求 project 的photoList
	$.ajax( {  
		    url:str_urlOfContractPhoto,// 跳转到 action  
		    data:object_postData,  
		    type:'post',  
		    cache:false,  
		    dataType:'json',  
		    beforeSend:function(XMLHttpRequest ){
		    	/*$.messager.progress({
					title : '提示',
					text : '正在关联合同信息~'
				});*/
		    },
		    success:function(obj_json) {
		    	//contractGrid.datagrid("load");
		    	//$.messager.progress('close');
		    	console.log(obj_json);
		    	if(obj_json.success){
		    	
		    		var array_contractPhoto=obj_json.data;
		    		var jq_tableContent=createTableContent(array_contractPhoto);
		    		$('#div-contractPhotoBody').append(jq_tableContent);

					$('.btn-contractPhoto').linkbutton({    
			    	    iconCls: 'icon-search'   
			    	}); 
		    		/*var jq_tableContent=$('#appendTableTest');
		    		$.each(obj_json.data,function(){
		    			var obj_contractPhoto=this;
		    			var jq_photoTable=jq_createSimplePhotoTable(obj_contractPhoto);
		    			var jq_div=$("<div></div>");
		    			jq_div.append(jq_photoTable);
		    			jq_tableContent.append(jq_div);
		    		});*/
		    	}
		    },  
		     error : function(data) { 
		    	 //console.log(data);
		    	// $.messager.progress('close');
		    	 alert("关联失败，请于管理员联系:18639551912。");
		     }  
		});
});



//绑定 删除contractPhoto 按钮
function deleteContractPhoto(contractPhotoId){
	var str_urlDeleteByContractPhotoId=contextPath+"/photo/operate/contractPhoto/delete.json";
	//alert("id= "+contractPhotoId);
	$.messager.confirm('确认','您确认想要删除照片吗？',function(r){    
	    if (r){
	    	var postData={'id':contractPhotoId};
	    	$.ajax( {  
	    	    url:str_urlDeleteByContractPhotoId,// 跳转到 action  
	    	    data:postData,  
	    	    type:'post',  
	    	    cache:false,  
	    	    dataType:'json',  
	    	    beforeSend:function(XMLHttpRequest ){
	    	    	/*$.messager.progress({
	    				title : '提示',
	    				text : '正在删除~'
	    			});*/
	    	    },
	    	    success:function(result) {
	    	    	var int_index=g_array_id_contractPhoto.indexOf(contractPhotoId);
	    	    	console.log("int_index " =int_index);
	    	    	console.log(result);
	    	    	if(result.success){
	    	    		refreshContractPhotoModule();
	    	    	}else{
	    	    		alert("数据加载失败，请于管理员联系:18639551912。");
	    	    	}

	    	    	//$.messager.progress('close');
	    	    },  
	    	     error : function(data) { 
	    	    	 //console.log(data);
	    	    	// $.messager.progress('close');
	    	    	 alert("数据加载失败，请于管理员联系:18639551912。");
	    	     }  
	    	});
	        //alert('确认删除');    
	    }else{
	    	//
	    	
	    }    
	}); 
}

//绑定 edtiPhoto 按钮 ,打开编辑对话框
function editContractPhoto(contractPhotoId){
	var postData={'id':contractPhotoId};
	var str_urlGetContractPhotoById=contextPath+'/photo/get/contractPhoto/'+contractPhotoId+'.json';
	var contractArray=ez_contractGrid.datagrid("getRows");

	if( !contractArray || contractArray.length==0){
		alert('未关联任何合同, 无法修改照片.');
	}

	$.ajax( {  
	    url:str_urlGetContractPhotoById,// 跳转到 action  
	    data:postData,  
	    type:'post',  
	    cache:false,  
	    dataType:'json',  
	    beforeSend:function(XMLHttpRequest ){
	    	/*$.messager.progress({
				title : '提示',
				text : '正在加载~'
			});*/
	    },
	    success:function(json) {
	    	console.log(json);
	    	if(json.success){
	    		var obj_contractPhoto=json.data;
	    		console.log(obj_contractPhoto);
	    		
	    		console.log(contractArray);

	    		 if(contractArray.length>0){
	  	    	   $('#input-contractPhotoEdit-contractSn').combobox({
	  	    		   valueField:'id',
	  	    		   textField:'sn',
	  	    		   data:contractArray
	  	    	   });
	  	    	   if(contractArray.length==1){
	  	    		   $('#input-contractPhotoEdit-contractSn').combobox('setValue',contractArray[0].id+'');
		  	    	   }
		  	       }else{
		  	    	   $('#input-contractPhotoEdit-contractSn').combobox({
		  	    		   disabled:true,
		  	    	   });
		  	       }


	    		var src=contextPath+'/resource/photo/contractPhoto/'+obj_contractPhoto.fileName;
	  	       	$('#img-contractPhotoEdit').attr({src:src});
	    		$('#form-contractPhotoEditDlg').form('load',obj_contractPhoto);
	  	       //设置contractSn select, 多个合同时为单选，单个合同，直接选好，无合同，关闭	
	    	}else{
	    		 alert("数据加载失败，请于管理员联系:18639551912。");
	    	}

	    	//$.messager.progress('close');
	    },  
	     error : function(data) { 
	    	 //console.log(data);
	    	 //$.messager.progress('close');
	    	 alert("数据加载失败，请于管理员联系:18639551912。");
	     }  
	});
	contractPhotoEditEzDlg.dialog('open');
	
}

function refreshContractPhotoModule(){
	$('#div-contractPhotoBody').empty();
	console.log("g_array_jq_td_contractPhoto.length="+g_array_jq_td_contractPhoto.length);
	
	var str_tableContent='<table class="table-pics"></table>';
	var jq_tableContent=$(str_tableContent);
	var str_tableBody='<tbody></tbody>';
	var jq_tableBody=$(str_tableBody);
	

	var int_rowCount;
	if(g_array_jq_td_contractPhoto.length==0){
		int_rowCount=0;
	}else{
		int_rowCount=parseInt((g_array_jq_td_contractPhoto.length-1)/3)+1;
	}

	for(var i=0;i<int_rowCount;i++){
		var str_tableTr='<tr></tr>'
		var jq_tableTr=$(str_tableTr);
		
		for(var j=0;j<3;j++){
			var int_index=i*3+j;
			if(int_index==g_array_jq_td_contractPhoto.length){
				break;
			}
			var jq_td_contractPhoto=g_array_jq_td_contractPhoto[int_index];
			
			jq_tableTr.append(jq_td_contractPhoto);
		}	
		jq_tableBody.append(jq_tableTr);
	}
	console.log(jq_tableBody.html());
	jq_tableContent.append(jq_tableBody);
	console.log(jq_tableContent.html());
	$('#div-contractPhotoBody').append(jq_tableContent);
}


function createTableContent(array_contractPhoto){
	var str_tableContent='<table class="table-pics"></table>';
	var jq_tableContent=$(str_tableContent);
	var str_tableBody='<tbody></tbody>';
	var jq_tableBody=$(str_tableBody);
	

	var int_rowCount;
	if(array_contractPhoto.length==0){
		int_rowCount=0;
	}else{
		int_rowCount=parseInt((array_contractPhoto.length-1)/3)+1;
	}

	for(var i=0;i<int_rowCount;i++){
		var str_tableTr='<tr></tr>'
		var jq_tableTr=$(str_tableTr);
		
		for(var j=0;j<3;j++){
			var int_index=i*3+j;
			if(int_index==array_contractPhoto.length){
				break;
			}
			var obj_contractPhoto=array_contractPhoto[int_index];
			console.log(obj_contractPhoto);
			var str_id=obj_contractPhoto.id;
			console.log(str_id);
			var jq_tableTd=$('<td id="td-contractPhoto-id-'+obj_contractPhoto.id+'"></td>'); 
			
			var jq_SimplePhotoTable=jq_createSimplePhotoTable(obj_contractPhoto);
			
			jq_tableTd.append(jq_SimplePhotoTable);

			g_array_jq_td_contractPhoto.push(jq_tableTd);
			g_array_id_contractPhoto.push(obj_contractPhoto.id);

			jq_tableTr.append(jq_tableTd);
		}	
		jq_tableBody.append(jq_tableTr);
	}
	jq_tableContent.append(jq_tableBody);
	console.log("g_array_jq_td_contractPhoto.length length = "+g_array_jq_td_contractPhoto.length.length);
	console.log("g_array_id_contractPhoto = ");
	console.log(g_array_id_contractPhoto);
	return jq_tableContent;
}

function refreshSimplePhotoTableByContractPhoto(obj_contractPhoto){
	
	var jq_table=jq_createSimplePhotoTable(obj_contractPhoto);
	var jq_tdContent=$('#td-contractPhoto-id-'+obj_contractPhoto.id);
	jq_tdContent.empty();
	jq_tdContent.append(jq_table);
	var str_jqSelector='#td-contractPhoto-id-'+obj_contractPhoto.id+' .btn-contractPhoto';
	$(str_jqSelector).linkbutton({    
		 iconCls: 'icon-search'   
	});
}


function jq_createSimplePhotoTable(obj_contractPhoto){
	console.log(obj_contractPhoto);
	var str_imgSrc=contextPath+'/resource/photo/contractPhoto/'+obj_contractPhoto.fileName;
	var str_table=['<table class="table-single-pic">',
					'  <tbody>',
					'    <tr>',
					'      <td colspan="2" align="center">',
					'        <div>',
					'          <img src="'+str_imgSrc+'" class="table-pics-img" /></div>',
					'      </td>',
					'    </tr>',
					'    <tr>',
					'      <td>',
					'        <div class="table-single-pic-prop-name">标题:</div></td>',
					'      <td>',
					'        <div class="table-single-pic-prop-value">'+obj_contractPhoto.title+'</div></td>',
					'    </tr>',
					'    <tr>',
					'      <td>',
					'        <div class="table-single-pic-prop-name">合同编号:</div></td>',
					'      <td>',
					'        <div class="table-single-pic-prop-value">'+obj_contractPhoto.contractSn+'</div></td>',
					'    </tr>',
					'    <tr>',
					'      <td>',
					'        <div class="table-single-pic-prop-name">备注:</div></td>',
					'      <td>',
					'        <div class="table-single-pic-prop-value">'+obj_contractPhoto.description+'</div></td>',
					'    </tr>',
					'    <tr>',
					'      <td colspan="2">',
					'        <div id=\'\' class="div-contractPhoto-btns">',
					'          <a id="btn3" href="#" class="btn-contractPhoto" data-options="iconCls:\'icon-search\'" onclick="javascript:editContractPhoto(\''+obj_contractPhoto.id+'\');">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
					'          <a id="btn4" href="#" class="btn-contractPhoto" data-options="iconCls:\'icon-search\'" onclick="javascript:deleteContractPhoto(\''+obj_contractPhoto.id+'\');">删除</a></div>',
					'      </td>',
					'    </tr>',
					'  </tbody>',
					'</table>'].join("");
	var jq_table=$(str_table);
/*
	'    <tr>',
	'      <td>',
	'        <div class="table-single-pic-prop-name">关键词:</div></td>',
	'      <td>',
	'        <div class="table-single-pic-prop-value">'+obj_contractPhoto.keyWord+'</div></td>',
	'    </tr>',*/
	return jq_table;
}

